package com.cityeye.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.cityeye.dao.DepartmentDao;
import com.cityeye.dao.IssueTypeDao;
import com.cityeye.dao.MmessageDao;
import com.cityeye.dao.PatrolDao;
import com.cityeye.dao.QueryDao;
import com.cityeye.dao.SubissuetypeDao;
import com.cityeye.entity.Department;
import com.cityeye.entity.Issuetype;
import com.cityeye.entity.Patrol;
import com.cityeye.entity.Subissuetype;
import com.cityeye.form.QueryForm;
import com.cityeye.util.QueryResult;

public class QueryAction extends MappingDispatchAction {

	public ActionForward queryStatistic(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		QueryForm qryForm = (QueryForm) form;	
		QueryDao dao= new QueryDao();
		
		qryForm.setMsgStatus(MmessageDao.STATE_COMPLETED_CONFIRMED);
		List<Object[]> successList = dao.queryAll(qryForm);
		
		qryForm.setMsgStatus(MmessageDao.STATE_COMPLETED_CONFIRMED_NEG);
		List<Object[]> failureList = dao.queryAll(qryForm);

		QueryResult qryResult = new QueryResult(successList,failureList);
		request.setAttribute("qryResult", qryResult);
		
		attachList(request);
		return mapping.findForward("success");
	}
	
	protected void attachList(HttpServletRequest request) {
		DepartmentDao deptDao = new DepartmentDao();
		List<Department> jieDepts = deptDao.findByProperty(Department.class,
				"type", DepartmentDao.TYPE_JIEDAO);
		request.setAttribute("jdlists", jieDepts);

		List<Department> quDepts = deptDao.findByProperty(Department.class,
				"type", DepartmentDao.TYPE_QU);
		request.setAttribute("qulists", quDepts);		

		PatrolDao patDao = new PatrolDao();
		List<Patrol> patrols = patDao.queryAll();
		request.setAttribute("patrols", patrols);

		SubissuetypeDao subissueDao = new SubissuetypeDao();
		List<Subissuetype> subIssues = subissueDao.queryAll();
		request.setAttribute("subissuetypes", subIssues);
		
		IssueTypeDao issueDao = new IssueTypeDao();
		List<Issuetype> issues = issueDao.queryAll();
		request.setAttribute("issuetypes", issues);		
	}	
}
